var api = require('../../../utils/api.js')
var $ = require('../../../utils/util.js')
var regeneratorRuntime = require('../../../utils/runtime.js')
var app = getApp()
var Store = app.Store

Page({
  data: {
    hasMore: true,// 判断 数据是否全部加载完
    isLodingLock: true, //锁 防止数据加载
    pageList: [], //分页数据列表
    pageNum: 1, //页码
    isShowKong: false, // 是否是空
    status:'', // 搜索关键字 
    currentTab: 0, // 当前选中的tab
    nav: [
      { name: '停电', type: 0, num: 0, color: '#dd524d' },
      { name: '送电', type: 1, num: 0,  color: '#16304f' }
    ], // tab
    isShowTime: false , // 是否显示时间弹框
    isLose: false, // 打开作废弹框
    isProcess: false,
    invalidReason: '', 
    steps:[
      {
        text: '发令人填写中',
      },
      {
        text: '发令人已提交',
      },
      {
        text: '操作班/监护人审核中',
      },
      {
        text: '审核通过',
      },
      {
        text: '操作进行中',
      },
      {
        text: '操作票完成',
      }
    ],
    statusArr:['全部','填写中','已提交','审核中','审核通过','进行中','已完成','已作废'],
    statusIdx:0,
    active:0,
    userRole:''
  },

  onLoad: function (options) {
    // this.getPagServer(1, true) //获取分页数据  
    this.setData({
      userRole: Store.getState().main.userRoleList.find((item)=>item=="operation_order_issuer")
    })
  },
  onShow: function () {
    this.getPagServer(1, true) //获取分页数据 
    api.$get(api.operateTicket.ticketCount).then((res) => {
      var nav = this.data.nav
      nav[0].num = res.data.powerOffNumber
      nav[1].num = res.data.powerOnNumber
      this.setData({nav})
    })
  },
  selectStatus(e){
    this.setData({
      status: e.detail.value==0?'':e.detail.value-1,
      statusIdx:e.detail.value
    })
    this.getPagServer(1, true)
  },
  onReachBottom() { //滑动到底部触发
    this.loadingMore()
  },
  onPullDownRefresh: function () { // 下拉刷新
    this.getPagServer(1, true) //获取分页数据
  },
  previewCurrent(e){
    var {item} = $.detail(e)
    if(!Boolean(item.imagePath)){
      $.toast('图片暂未生成，请联系管理员！')
      return false
    }
    wx.previewImage({
      current: '', // 当前显示图片的链接，不填则默认为urls的第一张
      urls: [item.imagePath+`?a=${Math.random()*100}`] // 需要预览的图片链接列表
    })
  },
  alertProcess(e){
    var {item} = $.detail(e)
    this.setData({
      isProcess: true,
      active: item.operationStatus
    })
  },
  onClose(){
    this.setData({
      isProcess: false,
    })
  },
  loadingMore: function () {  // 加载更多
    console.log(this.data.isLodingLock, 111)
    if (this.data.isLodingLock) {
      this.setData({
        isLodingLock: false
      })

      this.getPagServer()
    }
  },

  getPagServer(pageNum, isClickTab = false) { //分页  
    var params = {
      pageNum: pageNum == 1 ? 1 : this.data.pageNum,
      pageSize: 10, 
      operationType: this.data.currentTab,
      operationStatus: this.data.status 
    } 
    $.getPageList(api, this, api.operateTicket.operateTickList, params, isClickTab).then((res) => { 
      wx.stopPullDownRefresh() // 接口数据请求完停止刷新
    })
  },

  //##input 输入##
  input_handle(e) {
    this.setData({
      [e.currentTarget.dataset.key]: e.detail.value
    })
  },
  //##input 输入##

  keywordConfirmHandle(){ // 搜索输入框 
    // if($.isNull(this.data.keyword)){
      // $.toast('请输入内容') 
    // }else {
      this.getPagServer(1, true) //获取分页数据
    // }

  },

  tabHanlde(e) {
    var { index } = e.detail
    this.setData({
      currentTab: index
    })
    this.getPagServer(1, true) //获取分页数据
  },
  timeConfirmHandle(e){ // 时间组件传出来的数据
    var { time, key} = e.detail
    console.log(time, key)
  },
  openTimeHandle(e){ // 打开时间弹框
    this.setData({
      isShowTime: true
    })
  },
  openLoseHandle(e){ // 打开作废弹框
    $.linkurl(`/pages/lockMange/lockStation/lockStation?type=apply`) 
    // var {item} = $.detail(e)
    // this.setData({
    //   isLose: true,
    //   _id: item._id
    // })
  },
  setZuoFei(){//作废
    $.linkurl(`pages/lockMange/lockStation/lockStation?type=apply`) 
    if($.isNull(this.data.invalidReason)){
      $.toast("作废原因不能为空")
        return
    }
    var params = {
      "number": this.data.number,
      "state":'已作废',//状态变为 ：已作废
      "invalidReason":this.data.invalidReason,//作废原因
      "invalidPersonid":wx.getStorageSync('user_id'),//作废人id
      "invalidPerson":wx.getStorageSync('user_name'),//作废人
      "cancel":'1',
    }
    api.$post(api.defect.setZuoFei, params).then((res) => {
      $.toast('作废成功')
    })
  },
  addRiskMng(e){
    var {item} = $.detail(e)
    $.linkurl(`/pages/operationTicket/riskManage/riskManage?ticketNumber=${item.ticketNumber}`)
  },
  toOperateMake(e){
    var that=this
    const {key,item}=$.detail(e)
    if(key=='btn'){
      $.linkurl(`/pages/operationTicket/operationTicketMake/operationTicketMake?operationType=${that.data.currentTab}`)
      return false
    }
    wx.showModal({
      title: '被驳回原因',
      content: Boolean(item.approvalComments)?item.approvalComments:'流程不合规，无法完成',
      success (res) {
        if (res.confirm) {
          api.$get(api.operateTicket.ticketOperationStatus, {ticketNumber:item.ticketNumber}).then((res) => {
            $.linkurl(`/pages/operationTicket/operationTicketMake/operationTicketMake?ticketNumber=${item.ticketNumber}`)
          })
          
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  toOperateMake2(e){
    const {item}=$.detail(e)
    $.linkurl(`/pages/operationTicket/operationTicketMake/operationTicketMake?ticketNumber=${item.ticketNumber}`)
  },
  ticketEnd(e){
    var {item,type} = $.detail(e)
    $.linkurl(`/pages/operationTicket/beginOperate/beginOperate?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  toOpTicketPreview(e){ // 预览审核工作票
    var {item,type} = $.detail(e)
    $.linkurl(`/pages/operationTicket/operationTicketPreview/operationTicketPreview?ticketNumber=${item.ticketNumber}&type=${type}`)
  },
  beginOperate(e){
    var {item,type} = $.detail(e)
    $.linkurl(`/pages/operationTicket/beginOperate/beginOperate?ticketNumber=${item.ticketNumber}&type=${type}`)
  }
})